package com.example.app.ui.health.User;

import androidx.annotation.NonNull;

import java.util.Date;

/**
 * 健康数据模型
 * 包含心率、血氧、体温等健康指标数据
 */
public class HealthData {
    // 基本健康指标
    private final int heartRate;       // 心率 (BPM)
    private final float bloodOxygen;   // 血氧饱和度 (%)
    private final float temperature;   // 体温 (°C)
    private final float pressure;      // 气压 (hPa)
    
    // 数据质量相关
    private final boolean validHeartRate;    // 心率数据是否有效
    private final boolean validBloodOxygen;  // 血氧数据是否有效
    private final int signalQuality;         // 信号质量（0-100）
    
    // 元数据
    private final Date timestamp;      // 数据记录时间戳
    private final String deviceId;     // 设备ID
    
    // 原始数据
    private final int irValue;         // 红外光值
    private final int redValue;        // 红光值
    
    /**
     * HealthData构建器类
     */
    public static class Builder {
        // 必填项
        private final Date timestamp;
        
        // 可选健康指标（设置默认值）
        private int heartRate = 0;
        private float bloodOxygen = 0;
        private float temperature = 0;
        private float pressure = 0;
        
        // 可选数据质量指标（设置默认值）
        private boolean validHeartRate = false;
        private boolean validBloodOxygen = false;
        private int signalQuality = 0;
        
        // 可选元数据
        private String deviceId = "unknown";
        
        // 可选原始数据
        private int irValue = 0;
        private int redValue = 0;
        
        /**
         * 构造函数，初始化必填项
         */
        public Builder() {
            this.timestamp = new Date();
        }
        
        /**
         * 设置心率
         */
        public Builder heartRate(int heartRate, boolean isValid) {
            this.heartRate = heartRate;
            this.validHeartRate = isValid;
            return this;
        }
        
        /**
         * 设置血氧
         */
        public Builder bloodOxygen(float bloodOxygen, boolean isValid) {
            this.bloodOxygen = bloodOxygen;
            this.validBloodOxygen = isValid;
            return this;
        }
        
        /**
         * 设置体温
         */
        public Builder temperature(float temperature) {
            this.temperature = temperature;
            return this;
        }
        
        /**
         * 设置气压
         */
        public Builder pressure(float pressure) {
            this.pressure = pressure;
            return this;
        }
        
        /**
         * 设置信号质量
         */
        public Builder signalQuality(int signalQuality) {
            this.signalQuality = Math.min(100, Math.max(0, signalQuality));
            return this;
        }
        
        /**
         * 设置设备ID
         */
        public Builder deviceId(String deviceId) {
            if (deviceId != null && !deviceId.isEmpty()) {
                this.deviceId = deviceId;
            }
            return this;
        }
        
        /**
         * 设置原始传感器数据
         */
        public Builder rawData(int irValue, int redValue) {
            this.irValue = irValue;
            this.redValue = redValue;
            return this;
        }
        
        /**
         * 构建HealthData对象
         */
        public HealthData build() {
            return new HealthData(this);
        }
    }
    
    /**
     * 私有构造函数，通过Builder初始化
     */
    private HealthData(Builder builder) {
        this.heartRate = builder.heartRate;
        this.bloodOxygen = builder.bloodOxygen;
        this.temperature = builder.temperature;
        this.pressure = builder.pressure;
        this.validHeartRate = builder.validHeartRate;
        this.validBloodOxygen = builder.validBloodOxygen;
        this.signalQuality = builder.signalQuality;
        this.timestamp = builder.timestamp;
        this.deviceId = builder.deviceId;
        this.irValue = builder.irValue;
        this.redValue = builder.redValue;
    }
    
    /**
     * 获取心率值
     */
    public int getHeartRate() {
        return heartRate;
    }
    
    /**
     * 获取血氧值
     */
    public float getBloodOxygen() {
        return bloodOxygen;
    }
    
    /**
     * 获取体温值
     */
    public float getTemperature() {
        return temperature;
    }
    
    /**
     * 获取气压值
     */
    public float getPressure() {
        return pressure;
    }
    
    /**
     * 心率数据是否有效
     */
    public boolean isValidHeartRate() {
        return validHeartRate;
    }
    
    /**
     * 血氧数据是否有效
     */
    public boolean isValidBloodOxygen() {
        return validBloodOxygen;
    }
    
    /**
     * 获取信号质量
     */
    public int getSignalQuality() {
        return signalQuality;
    }
    
    /**
     * 获取时间戳
     */
    public Date getTimestamp() {
        return timestamp;
    }
    
    /**
     * 获取设备ID
     */
    public String getDeviceId() {
        return deviceId;
    }
    
    /**
     * 获取红外光值
     */
    public int getIrValue() {
        return irValue;
    }
    
    /**
     * 获取红光值
     */
    public int getRedValue() {
        return redValue;
    }
    
    /**
     * 数据是否为空（无心率和血氧数据）
     */
    public boolean isEmpty() {
        return !validHeartRate && !validBloodOxygen;
    }
    
    /**
     * 显示数据信息，用于调试
     */
    @NonNull
    @Override
    public String toString() {
        return "HealthData{" +
                "heartRate=" + heartRate +
                ", bloodOxygen=" + bloodOxygen +
                ", temperature=" + temperature +
                ", pressure=" + pressure +
                ", validHeartRate=" + validHeartRate +
                ", validBloodOxygen=" + validBloodOxygen +
                ", signalQuality=" + signalQuality +
                ", timestamp=" + timestamp +
                ", deviceId='" + deviceId + '\'' +
                '}';
    }
} 